<template>
	<qh-page-view title="商品详情">
		<block v-if="goods">
			<view class="flex-column-100">
				<scroll-view scroll-y class="scrollView goods-scroll-height">
					<swiper indicator-dots="true" indicator-active-color="var(--primary-color)" :autoplay="autoplay" :interval="interval" :duration="duration">
						<swiper-item v-if="video_url !=''">
							<video :src="video_url" id="myVideo" muted show-mute-btn autoplay="true" loop='false' class="myvideo" @ended="showBanner"></video>
						</swiper-item>
						<block v-for="(item, index) in imgUrls" :key="index">
							<swiper-item>
								<image :src="item" @tap="seeBig" :data-index="index" class="slide-image" mode="aspectFit">
								</image>
							</swiper-item>
						</block>
					</swiper>
					<!-- 中间-时间 -->
					<view class="countDown">
						<view class="countDownL">
							<image src="https://safood-1317438654.cos.ap-guangzhou.myqcloud.com/tempImage/1729071152670f8830e398a.png"></image>
							<text>限时火拼，超值低价</text>
						</view>
						<view class="countDownR">
							<block v-if="act_status==1">
								<view>距离活动开始还剩:</view>
								<view class="countDownR_T">
									<view v-if="countdown0>0">{{countdown0}}</view>
									<text v-if="countdown0>0">天</text>
									<view>{{countdown1}}</view>:<view>{{countdown2}}</view>:<view>{{countdown3}}</view>
								</view>
							</block>
							<block v-else-if="act_status==2">
								<view>距离活动结束还剩:</view>
								<view class="countDownR_T">
									<view v-if="countdown0>0">{{countdown0}}</view>
									<text v-if="countdown0>0">天</text>
									<view>{{countdown1}}</view>:<view>{{countdown2}}</view>:<view>{{countdown3}}</view>
								</view>
							</block>
							<block v-else>
								<view>活动已结束:</view>
								<view class="countDownR_T">
									<view>--</view>:<view>--</view>:<view>--</view>
								</view>
							</block>
						</view>
					</view>
					<!-- 显示部分详情 -->
					<view class="info">
						<view class="info-left">
							<view class="info-left-a">￥<text>{{actprice}}</text>
							</view>
							<view class="info-left-b">
								<view class="info-left-b1">￥{{goods_price}}</view>
								<view class="info-left-b2">{{group_num}}人拼团立省￥{{province}}</view>
							</view>
						</view>
						<view v-if="showShareBtn" class="details-share" @tap="showShare">
							<text class="qh-rt-single qh-rt-fenxiang1-copy share-icon"></text>
							<text class="text-share">分享</text>
						</view>
					</view>
					<!-- 活动名称 -->
					<view class="name">{{goods_name}}</view>
					<view class="language bg box-sizing" v-if="labelList.length">
						<block v-for="(item, index) in labelList" :key="index">
							<view class="languagelist">
								<block v-if="item.img">
									<image class="lang-image" :src="item.img"></image>
								</block>
								<block v-else>
									<icon type="success" size="14"></icon>
								</block>
								<text>{{item.label_name}}</text>
							</view>
						</block>
					</view>
					<!-- 拼团玩法介绍 -->
					<view class="rule">
						<view class="rule-left">邀请好友参团购买</view>
						<view class="rule-right" @tap="collageRules">
							<text>玩法详情</text>
							<image src="/static/images/arrow-right.png"></image>
						</view>
					</view>
					<!-- 去拼团 -->
					<view v-if="showMoreGroup" class="more-group-container">
						<view class='hide-more-group' @tap="hideMoreGroup"></view>
						<view class="more-group-box">
							<view class="more-group-close" @tap="hideMoreGroup">+</view>
							<view class="more-group-title">正在拼单</view>
							<view class="more-group-list" :style="all_group_num<=10?'padding-bottom:20rpx;':''">
								<view class="old-group-box" v-for="(item, index) in all_group" :key="index">
									<view>
										<image :src="item.avatar"></image>
										<text class="more-group-nickname">{{item.nickname}}</text>
									</view>
									<view>
										<text class="group-less-num-text">还差{{item.lack_number}}人</text>
										<view class="join-group" data-type="2" :data-billid="item.id" @tap="go_group">去参团
										</view>
									</view>
								</view>
							</view>
							<view v-if="all_group_num>10" class="more-group-footer">仅显示10人</view>
						</view>
					</view>
					<!-- 循环展示 -->
					<view v-if="all_group_num > 0 && initiator == 1">
						<view class="group-more">
							<text class="group-more-left">{{all_group_num}}人正在拼团，可直接参与</text>
							<view class="group-more-right" @tap="seeMoreGroup">
								<text>查看更多</text>
								<image src="/static/images/arrow.png"></image>
							</view>
						</view>
						<swiper class="old-group" indicator-active-color="#ed403f" autoplay="true" circular="true" vertical="true" interval="2000"
							duration="500">
							<swiper-item v-for="(item, index) in all_group" :key="index">
								<view class="old-group-box">
									<view>
										<image :src="item.avatar"></image>
										<text class="group-nickname">{{item.nickname}}</text>
									</view>
									<view>
										<text class="group-less-num-text">还差<text class='person-text-color'>{{item.lack_number}}</text>人拼成</text>
										<view class="join-group" data-type="1" :data-billid="item.id" @tap="go_group">去参团
										</view>
									</view>
								</view>
							</swiper-item>
						</swiper>
					</view>
					<!-- 配送方式 -->
					<view v-if="delivery_name" class="Specifications bg box-sizing" @tap="clickDeliveryRule">
						<text class="list-title" style="width: 68rpx;">配送</text>
						<view>
							<view class="fee-flex" :style="delivery_status == 1? 'margin-bottom : 20rpx' : ''">
								<text class="SpecificationsText delivery-title">配送方式</text>
								<text class="SpecificationsText">{{delivery_name}}</text>
								<block v-if="delivery_rule">
									<text v-if="delivery_rule.type==1" class="SpecificationsText">免运费</text>
									<block v-else>
										<text class="SpecificationsText before-line">快递费 ￥</text>
										<text v-if="delivery_rule.type==2||delivery_rule.type==3"
											class="SpecificationsText">{{delivery_rule.list[0].first_fee}}起</text>
										<text v-if="delivery_rule.type==4" class="SpecificationsText">{{delivery_rule.list[0].unified_fee}}起</text>
									</block>
									<view class="Specifications-img">
										<image mode="aspectFit" src="/static/images/arrow-1.png"></image>
									</view>
								</block>
							</view>
							<view v-if="delivery_status == 1">
								<text class="SpecificationsText delivery-title">配送费</text>
								<text class="SpecificationsText">单笔消费满{{delivery_cost}}元免运费</text>
							</view>
						</view>
					</view>
					<!-- 评价 -->
					<view class="CommentStyle bg" @tap="toComment">
						<view class="Comment-nav">
							<text class="Comment-nav-words">商品评价 ({{total}})</text>
							<view class="Specifications-img">
								<image mode="aspectFit" src="/static/images/arrow-1.png"></image>
							</view>
						</view>
						<view class="content">
							<view class="Comment-detail" v-if="total>0">
								<view class="userInfo">
									<image :src="commentInfo[0].avatar" mode="aspectFit" class="userImage"></image>
									<text class="nickName">{{commentInfo[0].nickname}}</text>
								</view>
								<view class="Comment-con">
									<text class="Comment-con-words">{{commentInfo[0].content}}</text>
									<view class="Comment-con-img">
										<image v-for="(item, i) in commentInfo[0].evaluate_img" :key="i" :src="item" mode="aspectFit" @tap.stop="scaleBig"
											:data-index="i"></image>
									</view>
								</view>
							</view>
							<view class="content-title" v-else>
								<text class="Comment-content">该商品暂无评价~</text>
							</view>
						</view>
					</view>
					<!-- 商品详情-->
					<view class="commodity-details" v-if="display_detail==1">
						<view class="commodity-details-title">商品详情</view>
						<view class="commodity-details-com">
							<jyf-parser :html="article_details"></jyf-parser>
						</view>
					</view>
					<!--底部留白  -->
					<view class="seize-a-seat"></view>
				</scroll-view>
			</view>
			<!-- 底部 -->
			<!-- 点击购买 -->
			<view class="buycar" :style="footerStyle">
				<view class="buycar-home" @tap="toHome">
					<image mode="widthFix" src="/static/images/buy-home.png"></image>
					<text>首页</text>
				</view>
				<view class="btn-box">
					<view class="group-btn singlebuy" @tap="purchase(1)" v-if="(userInfo.vid && userInfo.platform_type == 2) || (userInfo.platform_type == 1)">
						<view class="group-btn-price">￥{{goods_price}}</view>
						<view>单独购买</view>
					</view>
					<view class="group-btn singlebuy" v-if="!userInfo.vid && userInfo.platform_type == 2" :type="userInfo.login_state == 1 ? 4 : 3">
						<login-and-register @success="registerSuccess(1)">
							<view class="flex-cn-cn">
								<view class="group-btn-price">￥{{goods_price}}</view>
								<view>单独购买</view>
							</view>
						</login-and-register>
					</view>
					<block v-if="act_status==1">
						<view class="group-btn notTogetherbuy">
							<view>活动未开始</view>
						</view>
					</block>
					<block v-else-if="act_status==2">
						<view class="group-btn togetherbuy" @tap="purchase(2)"
							v-if="(userInfo.vid && userInfo.platform_type == 2) || (userInfo.platform_type == 1)">
							<view class="group-btn-price">￥{{actprice}}</view>
							<view>{{parent_id>0?'参与':'发起'}}拼团</view>
						</view>
						<view class="group-btn togetherbuy" v-if="!userInfo.vid && userInfo.platform_type == 2" :type="userInfo.login_state == 1 ? 4 : 3">
							<login-and-register @success="registerSuccess(2)">
								<view class="flex-cn-cn">
									<view class="group-btn-price">￥{{actprice}}</view>
									<view>{{parent_id>0?'参与':'发起'}}拼团</view>
								</view>
							</login-and-register>
						</view>
					</block>
					<block v-else>
						<view class="group-btn notTogetherbuy">
							<view>活动已结束</view>
						</view>
					</block>
				</view>
			</view>

			<!-- 购买弹出层 -->
			<view class="alert-mask" v-if="alertAppear==true">
				<!-- 背景层 -->
				<view @tap.stop="closeAlert" class='close-alert-style'></view>
				<!-- 弹出层 -->
				<!-- 正常购买 -->
				<view class="alert-com" v-if="clickStatus==1" :style="alertStyle">
					<view class="alert-product">
						<view class="productImg">
							<image mode="aspectFit" :src="mySkuImg" @tap="goods_sku_big_image"></image>
						</view>
						<view class="alert-product-info">
							<view class="productName">{{goods_name}}</view>
							<view>
								<text class="spellingNum">价格:</text>
								<text class="productPrice">￥{{goods_price}}</text>
							</view>
							<text class="stock">库存: {{myStock}}</text>
						</view>
					</view>
					<!-- 规格 -->
					<view class="alert-body">
						<view class="alert-color" v-if="attribute == 2">
							<view class="skuListCon" v-for="(item, mpIndex) in mySkuList" :key="mpIndex">
								<view class="skuListName">{{item.name}}</view>
								<block v-for="(item1, mcIndex) in item.list" :key="mcIndex">
									<block v-if="item1.status == 1">
										<text :class="'colorType ' + (mySkuSelectIndex[mpIndex]==mcIndex?'colorTypeActive':'')" @tap="checkMySku"
											:data-mp-index="mpIndex" :data-mc-index="mcIndex">{{item1.name}}</text>
									</block>
									<text v-else class="colorType mc-name-style">{{item1.name}}</text>
								</block>
							</view>
						</view>
						<view class="alert-shopNum">
							<view class="alertLimit">购买数量</view>
							<block v-if="attribute == 2 && mySelectSkuDetailId == -1">
								<view class="alert-payNum">
									<view class="alertBtn">
										<image src="/static/images/num-less-1.png"></image>
									</view>
									<view class="alertBtn alertNum">{{alertNum}}</view>
									<view class="alertBtn alert-plus">
										<image src="/static/images/num-add-1.png"></image>
									</view>
								</view>
							</block>
							<block v-else>
								<view class="alert-payNum">
									<view class="alertBtn" data-num="0" @tap.stop="payshopNum">
										<image src="/static/images/num-less-1.png"></image>
									</view>
									<view class="alertBtn alertNum">{{alertNum}}</view>
									<view class="alertBtn alert-plus" data-num="1" @tap.stop="payshopNum">
										<image src="/static/images/num-add-1.png"></image>
									</view>
								</view>
							</block>
						</view>
					</view>
					<view class="nextban" v-if="myStock < 1">下一步</view>
					<view class="nextbtn" v-else @tap="buynow">下一步</view>
					<text class="qh-rt-single qh-rt-a-ziyuan1 alert-close-icon" @tap.stop="closeAlert"></text>
				</view>

				<!-- 拼团购买 -->
				<view class="alert-com" v-else-if="clickStatus == 2" :style="alertStyle">
					<view class="alert-product">
						<view class="productImg">
							<image mode="aspectFit" :src="skuImg" @tap="group_sku_big_image"></image>
						</view>
						<view class="alert-product-info">
							<view class="productName">{{goods_name}}</view>
							<view>
								<text class="spellingNum">{{group_num}}人拼团价:</text>
								<text class="productPrice">￥{{actprice}}</text>
							</view>
							<text class="stock">库存: {{ stock }}</text>
						</view>
					</view>
					<!-- 规格 -->
					<view class="alert-body">
						<view class="alert-color" v-if="attribute == 2">
							<view class="skuListCon" v-for="(item, pIndex) in skuList" :key="pIndex">
								<view class="skuListName">{{item.name}}</view>
								<block v-for="(item1, cIndex) in item.list" :key="cIndex">
									<block v-if="item1.status==1">
										<text :class="'colorType ' + (skuSelectIndex[pIndex]==cIndex?'colorTypeActive':'')" @tap="checkSku"
											:data-p-index="pIndex" :data-c-index="cIndex">{{item1.name}}</text>
									</block>
									<text v-else class="colorType alert-name-color">{{item1.name}}</text>
								</block>
							</view>
						</view>
						<view class="alert-shopNum">
							<view class="alertLimit">
								<view class="alert-num">数量</view>
								<view class="limit" v-if="limit!=null && limit!=0">（限购 {{limit}} 件）</view>
							</view>
							<view class="alert-payNum-box">
								<view v-if="goodsDetails.rise_num!=0" class="limit limit-start">
									{{goodsDetails.rise_num}}件起购
								</view>
								<block v-if="attribute == 2 && selectSkuDetailId == -1">
									<view class="alert-payNum">
										<view class="alertBtn alert-reduce">
											<image src="/static/images/num-less-1.png"></image>
										</view>
										<view class="alertBtn alertNum">{{alertNum}}</view>
										<view class="alertBtn alert-plus">
											<image src="/static/images/num-add-1.png"></image>
										</view>
									</view>
								</block>
								<block v-else>
									<view class="alert-payNum">
										<view class="alertBtn alert-reduce" data-num="0" @tap.stop="payshopNum">
											<image src="/static/images/num-less-1.png"></image>
										</view>
										<view class="alertBtn alertNum">{{alertNum}}</view>
										<view class="alertBtn alert-plus" data-num="1" @tap.stop="payshopNum">
											<image src="/static/images/num-add-1.png"></image>
										</view>
									</view>
								</block>
							</view>
						</view>
					</view>
					<view v-if="stock < 1 || attribute==2 && selectSkuDetailId==-1" class="nextban">下一步</view>
					<view v-else class="nextbtn" @tap.stop="nextbtn">下一步</view>
					<text class="qh-rt-single qh-rt-a-ziyuan1 alert-close-icon" @tap.stop="closeAlert"></text>
				</view>

			</view>
			<!-- 配送规则 -->
			<view class="choose-sku-container sku-container-index" v-if="showDeliveryRule">
				<view class="choose-sku-model" @tap="clickDeliveryRule" @longtap="clickDeliveryRule" @touchmove="clickDeliveryRule"></view>
				<view class="choose-sku-box sku-box-bottom">
					<text class="qh-rt-single qh-rt-a-ziyuan1 alert-close-icon" @tap.stop="clickDeliveryRule"></text>
					
					<view class="model-title">运费计价说明</view>
					<view class="delivery-content">
						<view>
							<text>当前商品仅支持以下地区快递发货，如有疑问请联系商家</text>
						</view>
						<view v-for="(item, c) in delivery_rule.list" :key="c">
							<text>{{item.area_name}}，</text>
							<text v-if="delivery_rule.type==1">免运费</text>
							<text v-if="delivery_rule.type==2">首{{item.first_num*1}}件{{item.first_fee}}元，每续{{item.append_num*1}}件{{item.append_fee}}元。</text>
							<text
								v-else-if="delivery_rule.type==3">首{{item.first_num*1}}kg{{item.first_fee}}元，每续{{item.append_num*1}}kg{{item.append_fee}}元。</text>
							<text v-else-if="delivery_rule.type==4">统一运费{{item.unified_fee}}元。</text>
						</view>
						<view v-if="delivery_status == 1">单笔消费满{{delivery_cost}}元免运费</view>
					</view>
				</view>
			</view>
		</block>

		<!-- 引入分享模板 -->
		<share v-if='isShowShare' @close='closeShowShare' :share='share'></share>
	</qh-page-view>
</template>

<script>
	var app = getApp().globalData;
	import share from '@/components/share.vue'
	export default {
		components: {
			share,
		},
		data() {
			return {
				imgUrls: [], // 轮播图
				video_url: '',
				autoplay: false,
				interval: 5000,
				duration: 1000,
				sku: '',
				stock: 0,
				myStock: 0,
				countdown1: '',
				countdown2: '',
				countdown3: '',
				endDate2: '',
				goodsname: '',
				commentCom: [],
				alertAppear: false, //  弹层
				alertNum: 1,
				all_group: [],
				all_group_num: 0,
				initiator: 1,
				skuSelectIndex: [],
				selectSkuDetailId: -1,
				shopItemInfo: {},
				selectArr: [],
				skuImg: '',
				mySkuSelectIndex: [],
				mySelectSkuDetailId: -1,
				myShopItemInfo: {},
				mySelectArr: [],
				mySkuImg: '',
				labelList: [],
				parent_id: 0, // 判断开团参团
				click_type: 1, // 点击类型 1.被邀请，开团 2.去参团
				share: {
					maskShare: false, //分享弹窗
					maskHidden: false, //海报图片
					type: 0,
					data: '',
				},
				showDeliveryRule: false, //是否显示运费计算规则
				total: 0,
				showMoreGroup: false,
				clickStatus: 0,
				skuList: "",
				actprice: "",
				group_skuid: "",
				mySkuList: "",
				goods_price: "",
				countdown0: "",
				act_status: 0,
				limit: "",
				id: "",
				goodImage: "",
				actname: "",
				goods_name: "",
				attribute: "",
				province: "",
				group_num: "",
				item: "",
				start_time: "",
				end_time: "",
				gid: "",
				goods: 0,
				goodsDetails: "",
				delivery_set: "",
				delivery_rule: "",
				delivery_name: "",
				display_detail: 0,
				skuDetailList: "",
				mySkuDetailList: "",
				commentInfo: "",
				article_details: "",

				isShowShare: false,
				isShowShareData: {
					guide_id: null,
					goods: "",
					goods_img: '',
					goods_id: '0',
				},
				codeImg: '',
				delivery_status: 0,
				delivery_cost: '',
				isShowDialog: false,
				shoppingId: '',
				groupData: null,
				userInfo: {}, // 用户信息
				isGetPhone: false, // 是否获取手机号
				register_phone: '', // 注册手机号
				showShareBtn: true,

				browse_duration: 0, //浏览时长
				browseTimer: null, //浏览计时器
			};
		},
		computed: {
			footerStyle() {
				let h = this.$store.bottomHeight + 118
				return 'height:' + h + 'rpx;padding-bottom:' + this.$store.bottomHeight + 'rpx';
			},
			alertStyle() {
				return 'padding-bottom:' + (10 + this.$store.bottomHeight) + 'rpx';
			},
		},
		onShow() {
			if (this.browse_duration > 0) {
				this.browseTimer = setInterval(() => {
					this.browse_duration++;
				}, 1000)
			}
		},
		// 页面加载
		async onLoad(options) {
			// #ifdef MP-ALIPAY
			if (JSON.stringify(options) == '{}') {
				options = my.getLaunchOptionsSync().query;
			}
			// #endif
			console.log('页面参数：', options)
			await this.$onLaunched;
			
			// #ifdef MP-ALIPAY
			this.showShareBtn = app.XcxData.appid == app.XcxData.tempAppId ? false : true;
			// #endif
			
			/**
			 * &store_id => &s 门店id
			 * &distributor_id => &d 分销员id
			 * &share_uid => &su	分享人uid
			 * &share_guide_id => &sg	导购员id
			 * */
			// #ifdef MP-WEIXIN
			if (!options.id) {
				options = getApp().globalData.getOnLaunchParam(options);
			}
			// #endif
			if (options && (options.store_id || options.s)) {
				this.$store.store_id = options.store_id || options.s;
			}

			if (options && (options.share_uid || options.su)) {
				getApp().globalData.XcxData.share_uid = options.share_uid || options.su;
				uni.setStorageSync('share_uid', options.share_uid || options.su)
			}
			this.userInfo = {
				...app.XcxData.userInfo
			};
			this.setData({
				parent_id: options.parent_id ? options.parent_id : 0,
				id: options.id,
			});
			this.getDetail();
			if (!this.parent_id) {
				this.getAllGroupOrder();
			}
		},
		/**
		 * 生命周期函数--监听页面隐藏
		 */
		onHide() {
			clearInterval(this.browseTimer);
		},

		/**
		 * 生命周期函数--监听页面卸载
		 */
		onUnload() {
			clearInterval(this.browseTimer);
			app.addVisitGoodsRecord({
				goods_id: this.goodsDetails.gid,
				browse_duration: this.browse_duration,
			})
		},
		//  用户分享
		onShareAppMessage(res) {
			var id = this.id; // 活动id
			var actName = this.actname; // 活动名称
			console.log()
			return {
				title: actName,
				path: '/pages/index/Group-purchase-details/Group-purchase-details?id=' + id + '&store_id=' + this.$store.store_id + '&share_uid=' + app.XcxData
					.userInfo.uid,
				success: function(res) { // 转发成功
				},
				fail: function(res) { // 转发失败
				}
			};
		},
		methods: {
			//会员注册成功
			registerSuccess(type) {
				this.userInfo = {
					...app.XcxData.userInfo
				};
				this.purchase(type);
			},
			//轮播视频加载完触发事件
			showBanner() {
				this.autoplay = true;
			},
			//显示运费规则
			clickDeliveryRule: function() {
				if (this.delivery_rule) {
					this.setData({
						showDeliveryRule: !this.showDeliveryRule
					});
				}
			},
			closeShowShare() {
				this.setData({
					isShowShare: false,
					share: {
						maskShare: false
					},
				})
			},
			//点击分享
			showShare: function(e) {
				if (this.act_status == 1) {
					var text = "距开始：";
					var status = 1;
				} else if (this.act_status == 2) {
					var text = "距结束：";
					var status = 1;
				} else {
					var text = "活动已结束";
					var status = 2;
				}
				this.isShowShareData.groupData = {
					text: text,
					status: status,
					time: [this.countdown0, this.countdown1, this.countdown2, this.countdown3]
				}
				this.setData({
					share: {
						maskShare: true,
						maskHidden: false,
						data: this.isShowShareData,
						type: 2,
					},
					isShowShare: true,
				});
			},
			//查看大图
			seeBig: function(e) {
				var index = parseInt(e.currentTarget.dataset.index);
				uni.previewImage({
					current: this.imgUrls[index],
					urls: this.imgUrls // 需要预览的图片http链接列表

				});
			},
			// 拼团规格查看大图
			group_sku_big_image: function() {
				var group_sku_image_list = [];
				var list_length = group_sku_image_list.push(this.skuImg);

				if (list_length > 0) {
					uni.previewImage({
						current: this.skuImg,
						urls: group_sku_image_list // 需要预览的图片http链接列表

					});
				}
			},
			// 商品规格查看大图
			goods_sku_big_image: function() {
				var goods_sku_image_list = [];
				var list_length = goods_sku_image_list.push(this.mySkuImg);

				if (list_length > 0) {
					uni.previewImage({
						current: this.mySkuImg,
						urls: goods_sku_image_list // 需要预览的图片http链接列表

					});
				}
			},
			// 查看更多（正在拼单 弹框）
			seeMoreGroup: function() {
				this.setData({
					showMoreGroup: true
				});
				this.getAllGroupOrder();
			},
			// 关闭正在拼单弹框
			hideMoreGroup: function() {
				this.setData({
					showMoreGroup: false
				});
				this.getAllGroupOrder();
			},
			// 去参团
			go_group: function(e) {
				var parent_id = e.currentTarget.dataset.billid;
				var type = e.currentTarget.dataset.type;

				if (type == 2) {
					this.setData({
						showMoreGroup: false
					});
				} // 判断是否为规格

				this.setData({
					alertAppear: true,
					parent_id: parent_id,
					clickStatus: 2,
					alertNum: 1,
					click_type: 2
				});
			},
			// 拼团购买
			cratedSku: function() {
				var that = this;
				var skuDetailList = this.skuDetailList;
				var shopItemInfo = {};

				for (var i in skuDetailList) {
					shopItemInfo[skuDetailList[i].sku_id_list] = skuDetailList[i];
				}

				this.setData({
					shopItemInfo: shopItemInfo
				});
				this.checkItem();
			},
			checkSku: function(e) {
				var pIndex = e.currentTarget.dataset.pIndex;
				var cIndex = e.currentTarget.dataset.cIndex;
				var that = this;
				var skuList = this.skuList;
				var skuSelectIndex = this.skuSelectIndex;
				var selectArr = this.selectArr;

				if (selectArr[pIndex] != skuList[pIndex].list[cIndex].id) {
					selectArr[pIndex] = skuList[pIndex].list[cIndex].id;
					skuSelectIndex[pIndex] = cIndex;
				} else {
					selectArr[pIndex] = 0;
					skuSelectIndex[pIndex] = -1;
				}

				this.setData({
					selectArr: selectArr,
					skuSelectIndex: skuSelectIndex
				}); // console.log(skuSelectIndex);return

				this.checkItem();
			},
			checkItem: function() {
				var that = this;
				var option = this.skuList;
				var selectArr = this.selectArr;
				var result = []; //定义数组存储被选中的值

				for (var i in option) {
					result[i] = selectArr[i] ? selectArr[i] : 0;
				}

				for (var i in option) {
					var last = result[i];

					for (var k in option[i].list) {
						result[i] = option[i].list[k].id;
						option[i].list[k].status = this.isMay(result); //在数据里面添加字段isShow来判断是否可以选择
					}

					result[i] = last;
				}

				this.setData({
					skuList: option
				});
				var selectArr = this.selectArr;

				if (selectArr.indexOf(0) == -1) {
					var id_list = selectArr.join(',');
					var sku = this.shopItemInfo[id_list];
					this.setData({
						actprice: sku.act_price,
						selectSkuDetailId: sku.id,
						skuImg: sku.image,
						stock: sku.act_num,
						group_skuid: sku.id
					});
				} else {
					this.setData({
						actprice: this.actprice,
						selectSkuDetailId: -1,
						skuImg: this.goodImage,
						stock: this.stock
					});
				}
			},
			isMay: function(result) {
				for (var i in result) {
					if (result[i] == 0) {
						return 1;
					}
				}

				var sku = this.shopItemInfo[result];

				if (sku && sku.act_num >= 1) {
					return 1;
				} else {
					return 0;
				}
			},
			// 单独购买
			cratedMySku: function() {
				var that = this;
				var mySkuDetailList = this.mySkuDetailList;
				var myShopItemInfo = {};

				for (var i in mySkuDetailList) {
					myShopItemInfo[mySkuDetailList[i].sku_id_list] = mySkuDetailList[i];
				}

				this.setData({
					myShopItemInfo: myShopItemInfo
				});
				this.myCheckItem();
			},
			checkMySku: function(e) {
				var pIndex = e.currentTarget.dataset.mpIndex;
				var cIndex = e.currentTarget.dataset.mcIndex;
				var mySkuList = this.mySkuList;
				var mySkuSelectIndex = this.mySkuSelectIndex;
				var mySelectArr = this.mySelectArr;

				if (mySelectArr[pIndex] != mySkuList[pIndex].list[cIndex].id) {
					mySelectArr[pIndex] = mySkuList[pIndex].list[cIndex].id;
					mySkuSelectIndex[pIndex] = cIndex;
				} else {
					mySelectArr[pIndex] = 0;
					mySkuSelectIndex[pIndex] = -1;
				}

				this.setData({
					mySelectArr: mySelectArr,
					mySkuSelectIndex: mySkuSelectIndex
				});
				this.myCheckItem();
			},
			myCheckItem: function() {
				var option = this.mySkuList;
				var mySelectArr = this.mySelectArr;
				var result = []; //定义数组存储被选中的值

				for (var i in option) {
					result[i] = mySelectArr[i] ? mySelectArr[i] : 0;
				}

				for (var i in option) {
					var last = result[i];

					for (var k in option[i].list) {
						result[i] = option[i].list[k].id;
						option[i].list[k].status = this.myIsMay(result); //在数据里面添加字段isShow来判断是否可以选择
					}

					result[i] = last;
				}

				this.setData({
					mySkuList: option
				});

				if (mySelectArr.indexOf(0) == -1) {
					var id_list = mySelectArr.join(','); //skuImg
					console.log(id_list, 'id_list')
					var sku = this.myShopItemInfo[id_list];
					console.log(sku, 'sku')
					this.setData({
						goods_price: sku.online_price,
						myStock: parseInt(sku.goods_num),
						mySelectSkuDetailId: sku.id,
						mySkuImg: sku.goods_img
					});
				} else {
					this.setData({
						goods_price: this.goods_price,
						myStock: this.myStock,
						mySelectSkuDetailId: -1,
						mySkuImg: this.goodImage
					});
				}
			},
			myIsMay: function(result) {
				for (var i in result) {
					if (result[i] == 0) {
						return 1;
					}
				}

				var sku = this.myShopItemInfo[result];

				if (sku && sku.goods_num >= 1) {
					return 1;
				} else {
					return 0;
				}
			},

			//倒计时
			countTime() {
				var that = this;
				var date = new Date();
				var now = date.getTime();
				let end2 = that.endDate2.replace(/-/g, '/');
				var endDate = new Date(end2); //设置截止时间

				var end = endDate.getTime();
				var leftTime = end - now; //时间差

				var d, h, m, s, ms;

				if (leftTime >= 0) {
					d = Math.floor(leftTime / 1000 / 60 / 60 / 24); //天

					h = Math.floor(leftTime / 1000 / 60 / 60 % 24); //时

					m = Math.floor(leftTime / 1000 / 60 % 60); //分

					s = Math.floor(leftTime / 1000 % 60); //秒

					ms = Math.floor(leftTime % 1000); //毫秒

					ms = ms < 100 ? "0" + ms : ms;
					s = s < 10 ? "0" + s : s;
					m = m < 10 ? "0" + m : m;
					h = h < 10 ? "0" + h : h;
					that.setData({
						countdown0: d,
						countdown1: h,
						countdown2: m,
						countdown3: s
					}); //递归每秒调用countTime方法，显示动态时间效果

					setTimeout(that.countTime, 100);
				} else {
					var act_status = that.act_status;

					if (act_status == 1) {
						that.setData({
							act_status: 2,
							endDate2: that.end_time
						});
						that.countTime(); // 调用倒计时
					} else if (act_status == 2) {
						that.setData({
							act_status: 3,
							endDate2: that.start_time
						});
						return false;
					} else {
						return false;
					}
				}
			},

			// 回到首页
			toHome() {
				uni.reLaunch({
					url: '/pages/store/store'
				})
			},
			// 单独购买
			buynow() {
				if (this.myStock <= 0) {
					uni.showToast({
						title: '该商品库存不足',
						icon: 'none'
					});
					return false;
				} // 判断规格


				if (this.attribute == 2 && !this.mySkuDetailList) {
					uni.showToast({
						title: '请选择商品规格',
						icon: 'none'
					});
					return false;
				} //加入购物车


				var num = this.alertNum;
				var goods_id = this.gid;
				var that = this;
				getApp().globalData.sendRequest({
					url: '/Shopping/addShopping_v1',
					type: "post",
					data: {
						goods_id: goods_id,
						num: num,
						type: 2,
						sku_id: that.mySelectSkuDetailId > 0 ? that.mySelectSkuDetailId : ''
					},
					success: function(res) {
						if (res.status == 1) {
							that.shoppingId = res.id
							uni.navigateTo({
								url: '../../order/Settlement/Settlement?shopping_ids=' + JSON
									.stringify([res.id])
							});
						} else {
							uni.showToast({
								title: res.msg,
								icon: 'none',
								duration: 1500,
								mask: true
							});
						}
					}
				});
			},

			// 购买
			purchase(clickStatus) {
				// 1 表示单独购买myStock  2 表示发起/参与拼团stock
				if (clickStatus) {
					this.setData({
						clickStatus: clickStatus,
						alertAppear: true
					}); // 无规格 拼团单独购买 限购数量

					var attribute = this.attribute;

					if (clickStatus == 2) {
						var item = this.item;
						this.setData({
							alertNum: this.goodsDetails.rise_num != 0 ? this.goodsDetails.rise_num : 1,
							// 恢复购买数量
							limit: item
						});
					} else if (clickStatus == 1 && attribute == 1) {
						this.setData({
							alertNum: 1,
							// 恢复购买数量
							limit: item
						});
					}
				}
			},

			// 数量
			payshopNum(e) {
				var implement = e.currentTarget.dataset.num; //加 && 减

				var alertNum = parseFloat(this.alertNum); //购物数量

				var limit = this.limit; //限购数量

				var stock = this.stock; // 库存（拼团）

				var myStock = this.myStock; // 库存（单独）

				var clickStatus = this.clickStatus;

				if (implement == 0) {
					//减操作
					if (alertNum <= 1) {
						return;
					}

					if (clickStatus == 2 && this.goodsDetails.rise_num != 0 && alertNum <= parseFloat(this.goodsDetails
							.rise_num)) {
						//起购数量限制
						return;
					}

					alertNum--;
					this.alertNum = alertNum;
					this.setData({
						alertNum: alertNum
					});
				} else {
					//加操作
					if (clickStatus == 1) {
						if (alertNum >= myStock) {
							return;
						}

						alertNum++;
						this.alertNum = alertNum;
						this.setData({
							alertNum: alertNum
						});
					} else if (clickStatus == 2) {
						if (alertNum >= limit) {
							return;
						}

						if (alertNum >= stock) {
							return;
						}

						alertNum++;
						this.alertNum = alertNum;
						this.setData({
							alertNum: alertNum
						});
					}
				}
			},

			// 下一步
			nextbtn() {
				var data = {
					'group_id': this.id,
					'sku_id': this.group_skuid ? this.group_skuid : 0,
					'parent_id': this.parent_id ? this.parent_id : 0,
					'buy_number': this.alertNum
				};
				this.groupData = data

				uni.navigateTo({
					url: '/pages/order/paymentOrder/paymentOrder?data=' + JSON.stringify(data)
				});
			},

			// 关闭弹出
			closeAlert() {
				this.setData({
					alertNum: 1,
					alertAppear: false
				});

				if (this.click_type == 2) {
					this.setData({
						click_type: 1,
						parent_id: 0
					});
				}
			},

			// 玩法规则
			collageRules() {
				uni.navigateTo({
					url: '../collageRules/collageRules'
				});
			},

			// 拼团详情
			getDetail() {
				uni.showLoading({
					title: '加载中',
					// #ifndef MP-ALIPAY
					mask: true,
					// #endif
				});
				var that = this;
				getApp().globalData.sendRequest({
					url: '/Group/getGroupDetails_v1',
					data: {
						group_id: that.id
					},
					type: 'post',
					success: (res) => {
						uni.hideLoading();
						if (res.status == 1) {
							if (!this.$store.store_id) {
								this.$store.store_id = res.details_data.store_id;
							}
							// 时间 判断活动是否开始
							var d = new Date();
							var now_time = d.getTime() * 1;
							var start_time = res.details_data.start_time * 1000;
							var end_time = res.details_data.end_time * 1000;
							var acttype = parseInt(res.details_data.act_type);

							if (acttype == 1 && start_time > now_time) {
								that.setData({
									act_status: 1,
									endDate2: res.details_data.startTime
								});
							} else if (acttype == 1 && start_time <= now_time && end_time >= now_time) {
								that.setData({
									act_status: 2,
									endDate2: res.details_data.endTime
								});
							} else {
								that.setData({
									act_status: 3,
									endDate2: res.details_data.startTime
								});
							}

							that.countTime(); // 调用倒计时

							// 无规格商品库存
							if (res.details_data.attribute_state == 1) {
								that.setData({
									myStock: res.details_data.goods_num,
									stock: res.details_data.act_num
								});
							}

							that.setData({
								imgUrls: res.banner,
								video_url: res.details_data.video_url,
								// 轮播图           
								goodImage: res.details_data.goods_image,
								// 商品图片
								actname: res.details_data.act_name,
								// 活动名称
								goods_name: res.details_data.goods_name,
								// 商品名称
								attribute: res.details_data.attribute_state,
								// 商品类型 1.普通 2.规格
								actprice: res.details_data.act_price,
								// 拼团价格
								goods_price: res.details_data.online_price,
								// 活动价格
								province: res.details_data.province,
								// 差价
								group_num: res.details_data.number,
								// 参团人数
								item: res.details_data.item,
								// 限购
								limit: res.details_data.item,
								// 最大购买
								start_time: res.details_data.start_time,
								// 活动开始时间
								end_time: res.details_data.end_time,
								// 活动结束时间
								gid: res.details_data.gid,
								goods: 1,
								skuImg: res.details_data.goods_image,
								mySkuImg: res.details_data.goods_image,
								goodsDetails: res.details_data,
								delivery_set: res.delivery_set,
								delivery_rule: res.delivery_rule ? res.delivery_rule : null,
								isShowShareData: {
									guide_id: null,
									goods: res.details_data,
									goods_img: res.details_data.goods_image ? res.details_data.goods_image : '',
									goods_id: that.id,
								},
								delivery_status: res.delivery_status,
								delivery_cost: res.delivery_cost,

							});

							if (res.delivery_set) {
								var delivery_name = [];

								for (var i = 0; i < res.delivery_set.length; i++) {
									if (res.delivery_set[i].data == 1) {
										delivery_name.push(res.delivery_set[i].name);
									}
								}
								if (that.video_url) {
									that.autoplay = false
								} else {
									that.autoplay = true
								}
								that.setData({
									delivery_name: delivery_name.join(' & ')
								});
							}

							if (res.details_data.details) {
								//WxParse.wxParse('details', 'html', res.details_data.details, that, 5)
								setTimeout(() => {
									that.article_details = res.details_data.details;
								}, 200);
								that.setData({
									display_detail: 1 // 图文详情

								});
							} // 拼团规格

							if (res.skuDetailList && res.skuList) {
								var skuSelectIndex = [];
								var selectArr = [];

								for (var i = 0; i < res.skuList.length; i++) {
									skuSelectIndex.push(-1);
									selectArr.push(0);
								}
								let stock = 0
								res.skuDetailList.forEach(el => stock = stock + el.act_num)
								console.log(stock, res.skuDetailList)
								that.setData({
									skuList: res.skuList,
									skuDetailList: res.skuDetailList,
									stock: stock,
									skuSelectIndex: skuSelectIndex,
									selectArr: selectArr,
									skuImg: res.details_data.goods_image
								});
								that.cratedSku();
							}

							if (res.labelList) {
								that.setData({
									labelList: res.labelList
								});
							} // 单独购买规格

							if (res.mySkuDetailList && res.skuList) {
								var mySkuSelectIndex = [];
								var mySelectArr = [];

								for (var i = 0; i < res.skuList.length; i++) {
									mySkuSelectIndex.push(-1);
									mySelectArr.push(0);
								}
								let myStock = 0
								res.mySkuDetailList.forEach(el => myStock = myStock + el.goods_num)
								that.setData({
									mySkuList: res.skuList,
									mySkuDetailList: res.mySkuDetailList,
									myStock: myStock,
									mySkuSelectIndex: mySkuSelectIndex,
									mySelectArr: mySelectArr,
									mySkuImg: res.details_data.goods_image
								});
								that.cratedMySku();
							}

							this.browseTimer = setInterval(() => {
								this.browse_duration++;
							}, 1000)

							that.getcommentList();
						}
					}

				});
			},

			// 获取正在拼团
			getAllGroupOrder: function() {
				var that = this;
				getApp().globalData.sendRequest({
					url: '/Group/getAllGroupOrder',
					type: 'post',
					data: {
						group_id: that.id
					},

					success(res) {
						if (res.status == 1) {
							that.setData({
								all_group: res.group_order,
								all_group_num: res.count
							});
						}
					}

				});
			},
			//查看评价大图
			scaleBig: function(e) {
				var that = this;
				var index = parseInt(e.currentTarget.dataset.index);
				uni.previewImage({
					current: that.commentInfo[0].evaluate_img[index],
					urls: this.commentInfo[0].evaluate_img // 需要预览的图片http链接列表

				});
			},
			//获取商品评价
			async getcommentList() {
				const {
					status,
					data
				} = await this.$request('SmallProgramApi/OrderEvaluate/getGoodsEvaluate', {
					goods_id: this.gid
				})
				if (status == 1) {
					this.commentInfo = data.list;
					this.total = data.total;
				}
			},
			// 跳转到评价列表页
			toComment() {
				uni.navigateTo({
					url: '/pages/order/commentList/comment?goods_id=' + this.gid
				});
			},

		}
	};
</script>
<style>
	@import "./Group-purchase-details.css";

	.myvideo {
		width: 100%;
		height: 100%;
	}

	button::after {
		border-radius: 0;
	}
</style>
